﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class InvoiceDetailDAO
    {
        DataServices m_InvoiceDetailDAO = new DataServices();

        public DataTable GetAll()
        {
            m_InvoiceDetailDAO.Load("GetAllInvoiceDetail", null);
            return m_InvoiceDetailDAO;
        }

        public DataTable GetByInvoiceId(String invoiceId)
        {
            SqlParameter param = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param.Value = invoiceId;

            m_InvoiceDetailDAO.Load("GetDetailByInvoiceId", param);
            return m_InvoiceDetailDAO;
        }

        public DataTable GetFullByInvoiceId(String invoiceId)
        {
            SqlParameter param = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param.Value = invoiceId;

            m_InvoiceDetailDAO.Load("GetDetailFullByInvoiceId", param);
            return m_InvoiceDetailDAO;
        }

        public DataTable GetReportByInvoiceId(String invoiceId)
        {
            SqlParameter param = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param.Value = invoiceId;

            m_InvoiceDetailDAO.Load("GetDetailByInvoiceIdReport", param);
            return m_InvoiceDetailDAO;
        }

        public DataTable GetByMenuId(String menuId)
        {
            SqlParameter param = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param.Value = menuId;

            m_InvoiceDetailDAO.Load("GetDetailByMenuId", param);
            return m_InvoiceDetailDAO;
        }

        public DataTable GetByMenuAndInvoice(String invoiceId, String menuId)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = invoiceId;

            param[1] = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param[1].Value = menuId;

            m_InvoiceDetailDAO.Load("GetDetailByMenuAndInvoice", param);
            return m_InvoiceDetailDAO;
        }

        public DataTable GetByYear(int year)
        {
            SqlParameter param = new SqlParameter("@Year", SqlDbType.Int);
            param.Value = year;

            m_InvoiceDetailDAO.Load("GetAllInvoiceDetailYear", param);
            return m_InvoiceDetailDAO;
        }

        public DataTable GetTopMenuYear(int year)
        {
            SqlParameter param = new SqlParameter("@Year", SqlDbType.Int);
            param.Value = year;

            m_InvoiceDetailDAO.Load("GetTopMenuYear", param);
            return m_InvoiceDetailDAO;
        }

        public DataTable GetByMonth(int year, int month)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@Year", SqlDbType.Int);
            param[0].Value = year;

            param[1] = new SqlParameter("@Month", SqlDbType.Int);
            param[1].Value = month;

            m_InvoiceDetailDAO.Load("GetAllInvoiceDetailMonth", param);
            return m_InvoiceDetailDAO;
        }

        public DataTable GetTopMenuMonth(int year, int month)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@Year", SqlDbType.Int);
            param[0].Value = year;

            param[1] = new SqlParameter("@Month", SqlDbType.Int);
            param[1].Value = month;

            m_InvoiceDetailDAO.Load("GetTopMenuMonth", param);
            return m_InvoiceDetailDAO;
        }

        public int Add(InvoiceDetailInfo info)
        {
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param[1].Value = info.MenuId;

            param[2] = new SqlParameter("@State", SqlDbType.Bit);
            param[2].Value = info.State;

            param[3] = new SqlParameter("@Quantity", SqlDbType.Int);
            param[3].Value = info.Quantity;

            param[4] = new SqlParameter("@Price", SqlDbType.Money);
            param[4].Value = info.Price;

            param[5] = new SqlParameter("@IsDeleted", SqlDbType.Bit);
            param[5].Value = info.IsDeleted;

            return m_InvoiceDetailDAO.ExecuteUpdate("AddDetail", param);
        }

        public int Update(InvoiceDetailInfo info)
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param[1].Value = info.MenuId;

            param[2] = new SqlParameter("@Quantity", SqlDbType.Int);
            param[2].Value = info.Quantity;

            param[3] = new SqlParameter("@State", SqlDbType.Bit);
            param[3].Value = info.State;

            param[4] = new SqlParameter("@Price", SqlDbType.Money);
            param[4].Value = info.Price;

            return m_InvoiceDetailDAO.ExecuteUpdate("UpdateDetail", param);
        }

        public int Delete(InvoiceDetailInfo info)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param[1].Value = info.MenuId;

            return m_InvoiceDetailDAO.ExecuteUpdate("DeleteDetail", param);
        }

        public int DeleteByInvoice(String invoiceId)
        {
            SqlParameter param = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param.Value = invoiceId;

            return m_InvoiceDetailDAO.ExecuteUpdate("DeleteDetailByInvoice", param);
        }

        public DataRow AddNewRow()
        {
            return m_InvoiceDetailDAO.NewRow();
        }

        public void AddNewDetail(DataRow m_Row)
        {
            m_InvoiceDetailDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_InvoiceDetailDAO.ExecuteNoneQuery() > 0;
        }
    }
}
